package br.com.ffm.praticas.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import br.com.ffm.praticas.bean.ProductType;
import br.com.ffm.praticas.connection.ConnectionDB;

public class ProductTypeDAO {
	
	/**
	 * Method that returns all Product Types
	 * @return List<Product>
	 * @throws SQLException
	 */
	public List<ProductType> getProductTypes() throws SQLException {
		String query = "SELECT * FROM product_type ORDER BY description";
		List<ProductType> list = new LinkedList<ProductType>();
		ResultSet result = ConnectionDB.getInstance().executeReader(query);
		while (result.next()) {
			ProductType productType = new ProductType();
			productType.setId(result.getInt("id"));
			if (result.getString("description") != null)
				productType.setDescription(result.getString("description"));
			list.add(productType);
		}
		ConnectionDB.getInstance().close();
		return list;
	}
	
	/**
	 * Method that return a specific Product Type for your id
	 * @param id Identifier Product Type
	 * @return ProductType
	 * @throws SQLException
	 */
	public ProductType getProductType(int id) throws SQLException {
		String query = "SELECT * FROM product_type WHERE (id = " + id + ")";
		ProductType productType;
		ResultSet result = ConnectionDB.getInstance().executeReader(query);
		if (result.next()) {
			productType = new ProductType();
			productType.setId(result.getInt("id"));
			if (result.getString("description") != null)
				productType.setDescription(result.getString("description"));
			ConnectionDB.getInstance().close();
			return productType;
		}
		ConnectionDB.getInstance().close();
		return null;
	}
	
	/**
	 * Method that return a specific Product Type for your description
	 * @param description Description Product Type
	 * @return ProductType
	 * @throws SQLException
	 */
	public ProductType getProductType(String description) throws SQLException {
		String query = "SELECT * FROM product_type WHERE (description ='" + description + "')";
		ProductType productType;
		ResultSet result = ConnectionDB.getInstance().executeReader(query);
		if (result.next()) {
			productType = new ProductType();
			productType.setId(result.getInt("id"));
			if (result.getString("description") != null)
				productType.setDescription(result.getString("description"));
			ConnectionDB.getInstance().close();
			return productType;
		}
		ConnectionDB.getInstance().close();
		return null;
	}

}
